package demo.com.lgx.wechatfloatdemo;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.yhao.floatwindow.ActivityStack;
import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.IFloatWindow;
import com.yhao.floatwindow.MoveType;
import com.yhao.floatwindow.PermissionUtil;
import com.yhao.floatwindow.Screen;
import com.yhao.floatwindow.Util;
import com.yhao.floatwindow.ViewStateListenerAdapter;

/**
 * Created by Harry on 2018/8/8.
 * desc:
 */

public class SecondActivity extends BaseActivity {

    private ConstraintLayout parent;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initView();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startFloatWindow();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void startFloatWindow() {
        if (PermissionUtil.hasPermission(this)) {
            if (FloatWindow.get("cancel") == null) {
                int size = Util.dp2px(getApplicationContext(), 300);
                int screenWidth = Util.getScreenWidth(getApplicationContext());
                int screenHeight = Util.getScreenHeight(getApplicationContext());
                int xOffset = screenWidth - size / 2;
                //减去隐藏高度后，再减去状态栏高度
                int yOffset = Util.getScreenHeight(getApplicationContext()) - size / 2 - Util.getStatusBarHeight(getApplicationContext());
                PropertyValuesHolder showPvhX = PropertyValuesHolder.ofInt("x", screenWidth, xOffset);
                PropertyValuesHolder showPvhY = PropertyValuesHolder.ofInt("y", screenHeight, yOffset);
                ValueAnimator showAnimator = ObjectAnimator.ofPropertyValuesHolder(showPvhX, showPvhY);
                showAnimator.setDuration(300);
                FloatWindow.with(getApplicationContext())
                           .setTag("cancel")
                           .setView(R.layout.layout_window)
                           .setWidth(size)
                           .setHeight(size)
                           .setX(xOffset)
                           .setY(yOffset)
                           .setMoveType(MoveType.inactive, 0, 0)
                           .setCurrentActivityCount(ActivityStack.getInstance().getSize())
                           .setShowAnimator(showAnimator)
                           .setFilter(true, new Class[0])
                           .setDesktopShow(false)
                           .build();
            }

            if (FloatWindow.get("old") == null) {
                ImageView imageView = new ImageView(this);
                imageView.setBackgroundResource(R.mipmap.ic_launcher_round);
                final int size = Util.dp2px(getApplicationContext(), 80);
                FloatWindow.with(getApplicationContext())
                           .setTag("old")
                           .setView(imageView)
                           .setMoveType(MoveType.slide, 0, 0)
                           .setWidth(size)
                           .setHeight(size)
                           .setFilter(true, SecondActivity.class)
                           .setCurrentActivityCount(ActivityStack.getInstance().getSize())
                           .setX(Screen.width, 0.8f)  //设置控件初始位置
                           .setY(parent.getHeight() / 3)
                           .setParentHeight(parent.getHeight())
                           .setMoveStyle(150, new AccelerateInterpolator())
                           .setDesktopShow(false)
                           .setViewStateListener(new ViewStateListenerAdapter() {
                               private int downX = 0;
                               private int downY = 0;

                               @Override
                               public void onPositionUpdate(int motionEventAction, int x, int y) {
                                   if (motionEventAction == MotionEvent.ACTION_DOWN) {
                                       downX = x;
                                       downY = y;
                                   } else if (motionEventAction == MotionEvent.ACTION_MOVE) {
                                       if (Math.abs(x - downX) > 20 || Math.abs(y - downY) > 20) {
                                           if (FloatWindow.get("cancel") != null) {
                                               FloatWindow.get("cancel").show();
                                           }
                                       }
                                   } else if (motionEventAction == MotionEvent.ACTION_UP) {
                                       if (FloatWindow.get("cancel") != null) {
                                           FloatWindow.get("cancel").hide();
                                       }
                                       int screenWidth = Util.getScreenWidth(getApplicationContext());
                                       int screenHeight = Util.getScreenHeight(getApplicationContext());
                                       int pX = x + size / 2;
                                       int py = y + size / 2 + Util.getStatusBarHeight(getApplicationContext());
                                       int distance = (int) Math.sqrt((pX - screenWidth) * (pX - screenWidth) + (py - screenHeight) * (py - screenHeight));
                                       int radius = Util.dp2px(getApplicationContext(), 300) / 2;
                                       if (distance < radius) {
                                           FloatWindow.destroy("old");
                                           FloatWindow.destroy("cancel");
                                       }
                                   }
                               }
                           })
                           .build();
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(getApplicationContext(), WebViewActivity.class));
                    }
                });
                FloatWindow.get("cancel").init();
                FloatWindow.get("old").show();
            }
        } else {
            Toast.makeText(getApplicationContext(), "没有浮窗权限", Toast.LENGTH_SHORT).show();
        }
    }

    private void initView() {
        parent = findViewById(R.id.parent);
        button = findViewById(R.id.button);
    }
}
